//===============================================================================
// Microsoft patterns & practices Enterprise Library Contribution
// Resource Application Block
//===============================================================================

using System;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Configuration.Design;

#if !NUNIT
using Microsoft.VisualStudio.TestTools.UnitTesting;
#else
using NUnit.Framework;
using TestClass = NUnit.Framework.TestFixtureAttribute;
using TestInitialize = NUnit.Framework.SetUpAttribute;
using TestCleanup = NUnit.Framework.TearDownAttribute;
using TestMethod = NUnit.Framework.TestAttribute;
#endif

namespace EntLibContrib.Resource.Configuration.Design.Tests
{
	[TestClass]
	public class ResourceConfigurationDesignManagerFixture : ConfigurationDesignHost
	{
		protected override void InitializeCore()
		{
		}

		[TestMethod]
		public void RegisterTest()
		{
			ResourceConfigurationDesignManager manager = new ResourceConfigurationDesignManager();
			manager.Register(ServiceProvider);
		}

		[TestMethod]
		public void OpenAndSaveConfiguration()
		{
			ApplicationNode.Hierarchy.Load();
			Assert.AreEqual(0, ErrorLogService.ConfigurationErrorCount);
			ApplicationNode.Hierarchy.Open();
			Assert.AreEqual(0, ErrorLogService.ConfigurationErrorCount);

			ResourceManagerSettingsNode rootNode = (ResourceManagerSettingsNode)ApplicationNode.Hierarchy.FindNodeByType(typeof(ResourceManagerSettingsNode));
			Assert.IsNotNull(rootNode);
			Assert.AreEqual("ResourceManager1", rootNode.DefaultResourceManager.Name);

			Assert.AreEqual(1, ApplicationNode.Hierarchy.FindNodesByType(typeof(ResourceManagerSettingsNode)).Count);
			Assert.AreEqual(3, ApplicationNode.Hierarchy.FindNodesByType(typeof(ResourceManagerNode)).Count);
			Assert.AreEqual(3, ApplicationNode.Hierarchy.FindNodesByType(typeof(ResourceProviderNode)).Count);

			ApplicationNode.Hierarchy.Load();
			Assert.AreEqual(0, ErrorLogService.ConfigurationErrorCount);
			ApplicationNode.Hierarchy.Open();
			Assert.AreEqual(0, ErrorLogService.ConfigurationErrorCount);

			ApplicationNode.Hierarchy.Save();
			Assert.AreEqual(0, ErrorLogService.ConfigurationErrorCount);

			Assert.AreEqual(1, ApplicationNode.Hierarchy.FindNodesByType(typeof(ResourceManagerSettingsNode)).Count);
			Assert.AreEqual(3, ApplicationNode.Hierarchy.FindNodesByType(typeof(ResourceManagerNode)).Count);
			Assert.AreEqual(3, ApplicationNode.Hierarchy.FindNodesByType(typeof(ResourceProviderNode)).Count);
		}

		[TestMethod]
		public void BuildContextTest ()
		{
			ApplicationNode.Hierarchy.Load();
			Assert.AreEqual(0, ErrorLogService.ConfigurationErrorCount);
			ApplicationNode.Hierarchy.Open();
			Assert.AreEqual(0, ErrorLogService.ConfigurationErrorCount);
			
			IConfigurationSource source = ApplicationNode.Hierarchy.BuildConfigurationSource();
			Assert.IsNotNull(source.GetSection(ResourceManagerSettings.SectionName));
		}
	}
}